package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import cc.comac.mom.api.QualityAttachmentService;
import cc.comac.mom.entity.QualityAttachmentEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipRepairDoc;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.EquipRepairDocDao;
import cc.comac.mom.entity.EquipRepairDocEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipRepairDocDataHandler extends ServiceImpl<EquipRepairDocDao, EquipRepairDocEntity>{
	@Autowired	
	private EquipRepairDocDao equipRepairDocDao;
	@Autowired
	private QualityAttachmentService qualityAttachmentService;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	//批量新增	
	public ResultBean insertBatch(List<EquipRepairDoc>list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<EquipRepairDocEntity> listEntity = new ArrayList<EquipRepairDocEntity>();
		for(EquipRepairDoc item :list) {
			EquipRepairDocEntity innerData = item.getInnerData();
			QualityAttachmentEntity att = qualityAttachmentService.getByAttachmentId(item.getAttachmentId());
            if(att!=null){
				innerData.setDocumentName(att.getRealName());
				innerData.setDocumentSize(att.getFileSize());
				innerData.setDoucumentRealPath(att.getFilePath());
			}
			if(innerData == null) {
				throw new RuntimeException("数据为空");
			}
			if(innerData.getRepairId() == 0) {
				throw new RuntimeException("维修信息主键不存在");
			}
			baseEntityDataUtil.setBaseEntityData(innerData);
			innerData.setDocumentUploadUser(baseEntityDataUtil.getCurrentUser());
			innerData.setDocumentUploadUserJobNumber(baseEntityDataUtil.getJobNumber());
			listEntity.add(innerData);
		}
		boolean saveBatch = this.saveBatch(listEntity);
		if(!saveBatch) {
			throw new RuntimeException("保存维修文档失败");
		}
		return resultBean;
	}
	//修改
	public ResultBean update(EquipRepairDoc obj){
		ResultBean resultBean = new ResultBean(true);
		if(obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		EquipRepairDocEntity innerData = obj.getInnerData();
		if(innerData.getRepairId() == 0) {
			throw new RuntimeException("修改维修文档失败");
		}
		baseEntityDataUtil.updateBaseEntityData(innerData);
		int updateById = equipRepairDocDao.updateById(innerData);
		if(updateById<1) {
			throw new RuntimeException("修改维修文档失败");
		}
		return resultBean;
	}
	
	public ResultBean delete(EquipRepairDoc obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		EquipRepairDocEntity innerData = obj.getInnerData();
		EquipRepairDocEntity selectById = equipRepairDocDao.selectById(innerData.getId());
		if(selectById == null) {
			throw new RuntimeException("删除数据为空");
		}
		baseEntityDataUtil.deleteBaseEntityData(selectById);
		int updateById = equipRepairDocDao.updateById(selectById);
		if(updateById <1) {
			throw new RuntimeException("删除维修文档失败");
		}
		return resultBean;
	}
	//查询	
	public LimitPageBean<EquipRepairDocEntity> selectByCondition(EquipRepairDoc obj) {
    	QueryWrapper<EquipRepairDocEntity> queryWrapper = new QueryWrapper<EquipRepairDocEntity>();
    	queryWrapper.eq("row_status",1);
    	if(obj.getPageSize() == 0) {
    		obj.setPageSize(10);
    	}
		if(obj != null && obj.getInnerData() != null) {		
    		if(obj.getInnerData().getDocumentName() != null && !"".equals(obj.getInnerData().getDocumentName())) {
    			queryWrapper.like("document_name",obj.getInnerData().getDocumentName());
    		}
    		if(obj.getInnerData().getRepairId()!= 0) {
    			queryWrapper.eq("repair_id", obj.getInnerData().getRepairId());
    		}
		}
		Page<EquipRepairDocEntity> page = new Page<>(obj.getPageNum(),obj.getPageSize(),true);
		IPage<EquipRepairDocEntity> iPage = equipRepairDocDao.selectPage(page, queryWrapper);
		@SuppressWarnings("unchecked")
		LimitPageBean<EquipRepairDocEntity> limitPageBean = (LimitPageBean<EquipRepairDocEntity>) LimitPageBean.of(iPage);
		return limitPageBean;
		
	}
}
